import java.util.*;

public class Hand implements Comparable<Hand> {

	private List<Card> hand;

	Hand() {
		hand = new ArrayList<Card>(4);
	}

	Hand(Hand other) {
		this.hand = new ArrayList<Card>(4);

		try {
			for (Card c : other.getCards()) {
				hand.add(new Card(c.getValue(), c.getSuit()));
			}
		} catch (BadugiException ex) {

		}
	}

	public List<Card> getCards() {
		return this.hand;
	}

	public void add(Card c) {
		hand.add(c);
	}

	public void remove(int index) {
		hand.remove(index);
	}

	public Card getCard(int index) {
		return hand.get(index);
	}

	public int compareTo(Hand other) {
		Hand myHandCopy = new Hand(this);
		Hand otherHandCopy = new Hand(other);

		myHandCopy = this.evaluateBestHand(myHandCopy);
		otherHandCopy = other.evaluateBestHand(otherHandCopy);
		System.out.println("My hand score: " + myHandCopy.getCards().size()
				+ " other score: " + otherHandCopy.getCards().size());

		if (myHandCopy.getCards().size() - otherHandCopy.getCards().size() == 0) {

			int index = myHandCopy.getCards().size();

			do{
				index--;
			}while ((index > 0)
					&& (myHandCopy.getCard(index).compareTo(otherHandCopy.getCard(index)) == 0));
				

			return (-1)
					* myHandCopy.getCard(index).compareTo(
							otherHandCopy.getCard(index));

		}
		System.out.println("Hand size " + this.hand.size() + " copy size: "
				+ myHandCopy.getCards().size());
		System.out.println("O Hand size " + other.getCards().size()
				+ " copy size: " + otherHandCopy.getCards().size());

		return myHandCopy.getCards().size() - otherHandCopy.getCards().size();

	}

	public Hand evaluateBestHand(Hand hand) {

		Collections.sort(hand.getCards());

		int startIndex = 0;

		while (startIndex < hand.getCards().size() - 1) {
			System.out.println(hand.getCards().size());
			removeConflicts(hand, startIndex, 0);
			startIndex++;
		}

		// Collections.sort(this.hand);

		return hand;

	}

	private int removeConflicts(Hand newhand, int index, int doIConflict) {
		Card thisCard = newhand.getCard(index);
		Card otherCard;

		int startingIndex = index;

		/*
		 * if(index==hand.size()-1){ remove(index); return 1; }
		 */

		// System.out.println("Hello, I am "+thisCard.getValue()+" "+thisCard.getSuit());
		while (index < newhand.getCards().size() - 1) {

			otherCard = newhand.getCard(index + 1);

			if ((thisCard.getValue() == otherCard.getValue())
					|| (thisCard.getSuit() == otherCard.getSuit())) {
				// System.out.println(""+thisCard.getValue()+" "+thisCard.getSuit()+": I conflict with "+otherCard.getValue()+" "+otherCard.getSuit());

				 //System.out.println("");

				if (removeConflicts(newhand, index + 1, 1) == 0) {
					if (doIConflict == 1) {
						// System.out.println(""+thisCard.getValue()+" "+thisCard.getSuit()+": I delete myself");

						newhand.remove(startingIndex);
						return 1;
					} else {
						if (thisCard.getValue() == otherCard.getValue()) {
							newhand.remove(startingIndex);
							thisCard = newhand.getCard(startingIndex);
							return 1;
						} else {
							 //System.out.println(""+thisCard.getValue()+" "+thisCard.getSuit()+": bye "+otherCard.getValue()+" "+otherCard.getSuit());

							newhand.remove(index + 1);
						}

					}

				}
				;

			} else
				index++;

		}
		return 0;

	}

}
